﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class LeftSidebar
    {
        Form1 form1;
        Panel panel;

        private Label[] LabelLeftT = new Label[4];
        private Label[] LabelLeftB = new Label[1];

        private Label[] FloatLabel = new Label[2];

        public LeftSidebar(Panel _panel ,Form1 _form1)
        {
            form1 = _form1;
            panel = _panel;

            SetColor(panel.BackColor);

            for (int i = 0; i < LabelLeftT.Length; i++)
            {
                LabelLeftT[i] = new Label()
                {
                    Top = 83 * i,
                    Left = 3,
                    Size = new Size(79, 83),
                    BackColor = panel.BackColor,
                    Image = AllImage.ImgLft[i * 2 + 1]
                };

                LabelLeftT[i].MouseUp += new MouseEventHandler(LeftT_MouseUp);
                LabelLeftT[i].MouseDown += new MouseEventHandler(LeftT_MouseDown);
                LabelLeftT[i].MouseEnter += new EventHandler(LeftT_MouseEnter);
                LabelLeftT[i].MouseLeave += new EventHandler(LeftT_MouseLeave);
                
                panel.Controls.Add(LabelLeftT[i]);
            }

            for (int i = 0; i < LabelLeftB.Length; i++)
            {
                LabelLeftB[i] = new Label()
                {
                    Top = panel.Height - 83 * (i + 1),
                    Left = 3,
                    Size = new Size(79, 83),
                    BackColor = panel.BackColor,
                    Image = AllImage.ImgLftB[i * 2 + 1]
                };
                LabelLeftB[i].MouseDown += new MouseEventHandler(LeftB_MouseDown);
                LabelLeftB[i].MouseEnter += new EventHandler(LeftB_MouseEnter);
                LabelLeftB[i].MouseLeave += new EventHandler(LeftB_MouseLeave);

                panel.Controls.Add(LabelLeftB[i]);
            }

            FloatLabel[0] = new Label()
            {
                Top = 0,
                Left = -5,
                Size = new Size(3, 83),
                BackColor = Color.FromArgb(31, 111, 235),
            };
            panel.Controls.Add(FloatLabel[0]);

            FloatLabel[1] = new Label()
            {
                Top = 83,
                Left = -5,
                Size = new Size(3, 83),
                BackColor = Color.FromArgb(247, 129, 102),
            };
            panel.Controls.Add(FloatLabel[1]);
        }

        public void SetColor(Color color)
        {
            AllImage.SetColor(color);
            panel.BackColor = color;
        }

        public void SetSta(bool sta)
        {
            if (sta)
            {
                LabelLeftT[panel5_content].Image = AllImage.ImgLft[panel5_content * 2];
                FloatLabel[1].Location = new Point(0, LabelLeftT[panel5_content].Top);
            }
            else
            {
                LabelLeftT[panel5_content].Image = AllImage.ImgLft[panel5_content * 2 + 1];
                FloatLabel[1].Location = new Point(-5, LabelLeftT[panel5_content].Top);
            }
            FloatLabel[0].Location = new Point(-5, LabelLeftT[panel5_content].Top);
        }

        public void Rd_Bottom()
        {
            for (int i = 0; i < LabelLeftB.Length; i++)
            {
                LabelLeftB[i].Top = panel.Height - 83 * (i + 1);
            }
        }

        private int panel5_content = 0;
        private void LeftT_MouseUp(object sender, MouseEventArgs e)
        {
            Label label = (Label)sender;
            int content = 0;
            for (int i = 0; i < LabelLeftT.Length; i++)
            {
                if (label.Equals(LabelLeftT[i]))
                {
                    content = i;
                    FloatLabel[0].Location = new Point(-5, label.Top);
                    FloatLabel[1].Location = new Point(0, label.Top);
                }
            }


            if (!form1.panel5_vis || panel5_content != content)//从没有打开侧边框到打开  切换侧边框内容
            {
                form1.panel5_vis = true;
                panel5_content = content;
                content++;
                string str_name = "panel2" + content;

                foreach (Control control in form1.panel5.Controls)
                {
                    if (!control.Name.Equals(str_name)) control.Visible = false;
                    else control.Visible = true;
                }

                for (int i = 0; i < LabelLeftT.Length; i++)
                {
                    if (i == panel5_content)
                    {
                        LabelLeftT[i].Image = AllImage.ImgLft[i * 2];
                    }
                    else
                    {
                        LabelLeftT[i].Image = AllImage.ImgLft[i * 2 + 1];
                    }
                }

                FloatLabel[1].Location = new Point(0, label.Top);
            }
            else if (form1.panel5_vis && panel5_content == content)//关闭侧边框
            {
                form1.panel5_vis = false;
                FloatLabel[1].Location = new Point(-5, label.Top);
            }
            FloatLabel[0].Location = new Point(-5, label.Top);
        }
        private void LeftT_MouseDown(object sender, MouseEventArgs e)
        {
            Label label = (Label)sender;
            for (int i = 0; i < LabelLeftT.Length; i++)
            {
                if (label.Equals(LabelLeftT[i]))
                {
                    FloatLabel[0].Location = new Point(0, label.Top);
                    if(FloatLabel[1].Top == label.Top)
                        FloatLabel[1].Location = new Point(-5, label.Top);
                }
            }
        }
        private void LeftT_MouseEnter(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            for (int i = 0; i < LabelLeftT.Length; i++)
            {
                if (label.Equals(LabelLeftT[i]))
                {
                    label.Image = AllImage.ImgLft[i * 2];
                    return;
                }
            }
        }
        private void LeftT_MouseLeave(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            for (int i = 0; i < LabelLeftT.Length; i++)
            {
                if (label.Equals(LabelLeftT[i]))
                {
                    if(panel5_content != i || !form1.panel5_vis)
                        label.Image = AllImage.ImgLft[i * 2 + 1];
                    return;
                }
            }
        }


        private void LeftB_MouseDown(object sender, MouseEventArgs e)
        {
            
        }
        private void LeftB_MouseEnter(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            for (int i = 0; i < LabelLeftB.Length; i++)
            {
                if (label.Equals(LabelLeftB[i]))
                {
                    label.Image = AllImage.ImgLftB[i * 2];
                    return;
                }
            }
        }
        private void LeftB_MouseLeave(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            for (int i = 0; i < LabelLeftB.Length; i++)
            {
                if (label.Equals(LabelLeftB[i]))
                {
                    if (panel5_content != i || !form1.panel5_vis)
                        label.Image = AllImage.ImgLftB[i * 2 + 1];
                    return;
                }
            }
        }
    }
}